/*
 * =====================================================================================
 *
 *       Filename:  thread.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  06/30/2012 12:58:50 PM
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  wangtengchao , wangtengchao AT gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef THREAD_H
#define THREAD_H
#include<pthread.h>
#include<boost/bind.hpp>
#include<boost/function.hpp>
#include<boost/noncopyable.hpp>

namespace inm
{
	namespace concurrency
	{
		class Thread:boost::noncopyable
		{
			public:
				typedef boost::function<void ()> ThreadFunc;
				explicit Thread(const ThreadFunc& func):threadFunc_(func){}
				void start()
                {//thread start after start()
                    pthread_create(&pid,NULL,&threadFunc,this);
				}
                void join()
                {
                    pthread_join(pid,NULL);
                }
			private:
				static void* threadFunc(void* obj)
				{//for compatible with pthread_create
					Thread* thread = static_cast<Thread*>(obj);	
					thread->run();
				}
				void run()
				{//the actual thread function to be runned
                 //catche exception if needed
					threadFunc_();		
				}
				ThreadFunc threadFunc_;
				pthread_t pid;
		};
	};

};
#endif //THREAD_H
